package com.example.security_demo.config;

import com.example.security_demo.filter.RequestWrapperFilter;
import com.example.security_demo.handler.SecurityAuthenticationFailureHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.Filter;

/**
 * 出现了循环依赖
 *
 * @author zzh
 * @date 2022/4/29
 * @since 1.0
 */
@Configuration
@Import(WebSecurityConfiguration.class)
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Autowired
    private SecurityAuthenticationFailureHandler authenticationFailureHandler;


//    @Bean
//    public FilterRegistrationBean<Filter> codeValidationFilter() {
//
//        FilterRegistrationBean<Filter> codeValidationFilterBean = new FilterRegistrationBean<>();
//        codeValidationFilterBean.addUrlPatterns("/*");
//        codeValidationFilterBean.setOrder(-1000);
//        codeValidationFilterBean.setFilter(new SpringSecurityTestFilter(authenticationFailureHandler));
//        return codeValidationFilterBean;
//    }

    @Bean
    public FilterRegistrationBean<Filter> requestWrapperFilter() {
        FilterRegistrationBean<Filter> requestWrapperFilterBean = new FilterRegistrationBean<>();
        requestWrapperFilterBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        requestWrapperFilterBean.setFilter(new RequestWrapperFilter());
        requestWrapperFilterBean.addUrlPatterns("/*");

        return requestWrapperFilterBean;
    }
}
